package game;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

public class Trex extends Sprite{

    private BufferedImage[] trexs;



    @Override
    public void init() {
        super.x = 90;
        super.y = 90;
        super.image = ImageManager.loadImage("src/Images/"+ GameData.Skin +"/trex0.png");
        super.width = super.image.getWidth();
        super.height = super.image.getHeight();
        trexs = new BufferedImage[8];
        for (int i = 0; i < 8; i++) {
            trexs[i] = ImageManager.loadImage("src/Images/"+ GameData.Skin +"/trex"+i+".png");
        }
        super.speed = 3;
    }
    private boolean flag;
    private int cnt;
    private int state = 1;
    public void getState(int x){
        this.state = 2;
    }
    //1 待机动画0,1
    //2 奔跑动画2，3
    //3 死亡动画4，5
    //4 冲刺动画6，7
    @Override
    public void draw(Graphics g) {
        offscreenImage = new BufferedImage(600,200,BufferedImage.TYPE_4BYTE_ABGR);
        offscreenGraphics = offscreenImage.createGraphics();
        switch (state){
            case (1)->{
                this.image = trexs[0];
                super.draw(offscreenGraphics);
            }
            case (2)->{
                if(cnt<24){
                    if(flag){
                        this.image = trexs[2];
                    }else {
                        this.image = trexs[3];
                    }
                    super.draw(offscreenGraphics);
                    cnt++;
                }else {
                    flag = !flag;
                    cnt = 0;
                }
            }
            case (3)->{
                if(cnt<24){
                    if(flag){
                        this.image = trexs[4];
                    }else {
                        this.image = trexs[5];
                    }
                    super.draw(offscreenGraphics);
                    cnt++;
                }else {
                    flag = !flag;
                    cnt = 0;
                }
            }
            case (4)->{
                if(cnt<24){
                    if(flag){
                        this.image = trexs[6];
                    }else {
                        this.image = trexs[7];
                    }
                    super.draw(offscreenGraphics);
                    cnt++;
                }else {
                    flag = !flag;
                    cnt = 0;
                }
            }
        }
        g.drawImage(offscreenImage,0,0,null);
    }
    private int updown = -1;
    private boolean jump = false;
    public void jumpstate(int code){
        if(code == KeyEvent.VK_SPACE && GameData.gameState == 2&&!jump){
            GameManager.getInstance().gameSound.hit.PlaySound();
            jump = true;
            GameData.All_jump++;
        }
    }//按下空格键跳跃
    private boolean bowdown_flag;
    public void bowdown(int code,int type){
        if (code == KeyEvent.VK_DOWN && GameData.gameState == 2){
            if (type == 1){
                this.state = 4;
                bowdown_flag = true;
            }else {
                this.state = 2;
                bowdown_flag = false;
            }
        }
    }
    public boolean skill;
    public void disappear(int code){
        if (code == KeyEvent.VK_X){
            skill = !skill;
        }
    }
    @Override
    public void move() {
        if (jump){
            if (bowdown_flag){
                this.updown = 1;
            }//俯冲下落
            if(super.y<=10){
                this.updown = 1;
            }//自然下落
            if (super.y > 90){
                this.updown = -1;
                jump = false;
            }//停止下落
            super.y = super.y + super.speed*this.updown;
            //上升
        }

    }

    @Override
    public Rectangle getBounds() {
        if(skill){

            return new Rectangle(0,0,0,0);
        }
        if(this.state == 4){
            return new Rectangle(x+10 ,y+20,width-30,height-30);
        }
        return new Rectangle(x+12,y+5,width-20,height-18);
    }

    @Override
    public void destroy() {

    }
}
